﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBOffice.DAL;

namespace PBOffice.BLL
{
    public class NoteManagerBLL
    {
        #region 根据用户名查询便签信息
        /// <summary>
        /// 根据用户名查询便签信息
        /// </summary>
        /// <returns></returns>
        public List<Note> ShowNote(int userNo)
        {
            using (MyOfficeEntities myoffigc = new MyOfficeEntities())
            {
                var onte = from v in myoffigc.Notes
                           where v.User.UserNo==userNo
                           select v;
                return onte.ToList();
            }
        }
        #endregion

        #region 根据便签编号查询便签
        /// <summary>
        /// 根据便签编号查询便签
        /// </summary>
        /// <param name="noteNo"></param>
        /// <returns></returns>
        public List<Note> GetNote(int noteNo)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var note = from c in myoffice.Notes
                           where c.NoteNo == noteNo
                           select c;
                return note.ToList();
            }
        } 
        #endregion


        #region 添加便签
        /// <summary>
        /// 添加便签
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public bool AddNote(Note n)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                Note note = new Note()
                {
                    NoteTitle = n.NoteTitle,
                    NoteContent = n.NoteContent,
                    NoteCreateTime = n.NoteCreateTime,
                    User = n.User
                };
                myoffice.Notes.AddObject(note);//添加便签信息。
                return myoffice.SaveChanges() > 0; //添加成功为true，否则为false。
            }

        }
        #endregion


        #region 修改便签
        /// <summary>
        /// 修改便签
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public bool UpdateNote(Note n)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var unote = from c in myoffice.Notes
                            where c.NoteNo == n.NoteNo
                            select c;
                foreach (var item in unote)
                {
                    item.NoteTitle = n.NoteTitle;
                    item.NoteContent = n.NoteContent;
                    item.NoteCreateTime = n.NoteCreateTime;
                    item.User.UserNo = n.User.UserNo;
                }
                return myoffice.SaveChanges() > 0;//修改成功返回true，否则返回false
            }

        }

        #endregion

        #region 删除便签
        /// <summary>
        ///根据便签编号删除便签
        /// </summary>
        /// <param name="noteNo"></param>
        /// <returns></returns>
        public bool DeleteNote(int noteNo)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var dnote = from c in myoffice.Notes
                            where c.NoteNo == noteNo
                            select c;  //查找指定便签
                //dnote.First();
                //Note no = (Note)dnote;
                foreach (var item in dnote)
                {
                    myoffice.Notes.DeleteObject(item);//删除指定便签
                }

                return myoffice.SaveChanges() > 0;//删除成功，返回true，否则返回false
            }
        }
        #endregion
    }
}
